//
//  XLGCheckNewVersionInternal.m
//  etionCBLogic
//
//  Created by wjxfhxy on 15-4-17.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGCheckNewVersionInternal.h"

#import "CGWService+ChannelProocol.h"

#import "XLGCheckNewVersionIntermediate.h"

@implementation XLGCheckNewVersionInternal
{
    XLGCheckVersionChannelProtocol *_channelProtocol;
}

- (id)init
{
    self = [super init];
    
    _channelProtocol = [XLGCheckVersionChannelProtocol new];
    
    return self;
}

- (id)initWithDataOperationManagerDelegate:(id<XMSDataOperationManagerDelegate>)domdelegate
{
    self = [super initWithDataOperationManagerDelegate:domdelegate];
    
    return self;
}

- (id)initWithDataOperationManagerDelegate:(id<XMSDataOperationManagerDelegate>)domdelegate AttachDOM:(XMSDataOperationManager *)attachdom
{
    self = [super initWithDataOperationManagerDelegate:domdelegate AttachDOM:attachdom];
    
    return self;
}

- (void)dealloc
{
    [_channelProtocol release_mm];
    superdealloc_mm;
}

- (XLGResult*)checkVersion:(CProtocolBase*(^)())channelProtocol
{
    XLGUpdateInfoObj *update = nil;
    NSError *error = nil;
    NSInteger nResult = FALSE;
    if ([XLGContext context].m_bIsoffline == NO)
    {
        XLGResult *result = [self.service sendChannelProtocol:channelProtocol];
        error = result.outError;
        
        if (result.outResult == YES)
        {
            update = (XLGUpdateInfoObj*)result.outObj;
            if (update != nil)
            {
                if (update.enforceUpdate == YES)
                    nResult = XLGCheckNewVersionForceUpdateCode;
                else if (update.buildNo > [XLGContext context].m_nBuildno)
                    nResult = XLGCheckNewVersionSuggestUpdateCode;
                else
                    nResult = YES;
            }
        }
        else if (nResult != PROTOCOLRETOBJNIL&& nResult != TERMINATEREADWRITE)
            nResult = NO;
    }
    else
        nResult = NETWORKOFFLINE;
    
    XLGResult *r = [XLGResult resultWithOutResult:nResult outObj:update outError:error];
    
    return r;
}

- (XLGResult*)checkVersion
{
    return [self checkVersion:^CProtocolBase *
    {
        return [_channelProtocol checkVersionProtocol];
    }];
}

- (XLGResult*)checkVersionAndAuthentication
{
    return [self checkVersion:^CProtocolBase *
    {
         return [_channelProtocol checkVersionAndAuthenticationProtocol];
    }];
}


@end
